package demoa.yls.example.com.android_dialog_box;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimplev7Dialog();
//                showSimpleAppDialog();
                showInputDialog();
            }
        });
        findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimplev7Dialog();
//                showSimpleAppDialog();
                showInputDialog();
            }
        });
        findViewById(R.id.btn_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimplev7Dialog();
//                showSimpleAppDialog();
                showInputDialog();
            }
        });
        findViewById(R.id.btn_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimplev7Dialog();
//                showSimpleAppDialog();
                showInputDialog();
            }
        });
        findViewById(R.id.btn_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimplev7Dialog();
//                showSimpleAppDialog();
                showInputDialog();
            }
        });
    }

    private void showInputDialog() {
        final EditText editText1=new EditText(MainActivity.this);
        final EditText editText2=new EditText(MainActivity.this);
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog,null,false);

        final TextView nameView=view.findViewById(R.id.edt_name);
        final TextView ageView=view.findViewById(R.id.edt_age);

        new AlertDialog.Builder(MainActivity.this).setTitle("请输入姓名")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String name =nameView.getText().toString();
                        int age =Integer.parseInt(ageView.getText().toString());

                        Toast.makeText(MainActivity.this,"欢迎光临"+name+"  "+age,Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }
/*

    private void showSimpleAppDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("这是标题");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setMessage("这是消息");

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,"positive:"+i,Toast.LENGTH_SHORT).show();
            }
        });
        builder.create();
        builder.show();
    }
*/

    private void showSimplev7Dialog() {
        final String[] cities={"上海","广州","梅州","汕头"};
        final String[] xingbie={"男","女"};
        final String[] favorite={"薛之谦","邓紫棋","张学友","张敬轩","薛凯琪"};
        final boolean[] isChecked= new boolean[5];
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("选择城市")

                .setMultiChoiceItems(favorite, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        isChecked[i]=b;
                        if(b){
                            Toast.makeText(MainActivity.this,"你选中了"+favorite[i],
                                    Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(MainActivity.this,"你取消了"+favorite[i],
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                })
//               .setSingleChoiceItems(xingbie, 0, new DialogInterface.OnClickListener() {
//                   @Override
//                   public void onClick(DialogInterface dialogInterface, int i) {
//                       Toast.makeText(MainActivity.this,"你的性别是"+xingbie[i],
//                               Toast.LENGTH_SHORT).show();
//                       dialogInterface.cancel();
//                   }
//               })
//               .setItems(cities, new DialogInterface.OnClickListener() {
//                   @Override
//                   public void onClick(DialogInterface dialogInterface, int i) {
//                       Toast.makeText(MainActivity.this,"你选择了"+cities[i],
//                               Toast.LENGTH_SHORT).show();
//                   }
//               })
                .setIcon(R.mipmap.ic_launcher)
//                    .setMessage("这是消息")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        StringBuffer buffer =new StringBuffer();

                        for(int m= 0;m<isChecked.length;m++){
                            if (isChecked[m]){
                                buffer.append(favorite[m]);
                            }
                        }
                        Toast.makeText(MainActivity.this,"你喜欢的明星有"+buffer,Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }
}

